package com.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.store.configure.Result;
import com.store.entity.Food;
import com.store.entity.NoticeUser;
import com.store.entity.NoticeUser;
import com.store.entity.SaleLog;
import com.store.mapper.*;
import com.store.service.NoticeUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.store.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2025-01-04
 */
@Service
public class NoticeUserServiceImpl extends ServiceImpl<NoticeUserMapper, NoticeUser> implements NoticeUserService {

    @Resource
    NoticeUserMapper noticeUserMapper;

    @Override
    public Result<NoticeUser> updateNoticeUser(NoticeUser noticeUser) {
        Result<NoticeUser> result = new Result<>();
        try {
            if (StringUtils.isEmpty(noticeUser.getNoticeUserId())) {
                //如果ID为空，代表添加
                noticeUser.setNoticeUserId(IDGenerator.getUUID("NOTICEUSER"));
                noticeUserMapper.insert(noticeUser);
            } else {
                //如果ID不为空，代表修改
                noticeUserMapper.updateById(noticeUser);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<NoticeUser> delNoticeUser(NoticeUser noticeUser) {
        Result<NoticeUser> result = new Result<>();
        try {
            if (StringUtils.isEmpty(noticeUser.getNoticeUserId())) {
                result.setMessage("noticeUserId can't be empty");
                return result;
            }
            noticeUserMapper.deleteById(noticeUser.getNoticeUserId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<NoticeUser> batchDelNoticeUser(NoticeUser noticeUser) {
        Result<NoticeUser> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(noticeUser.getIds())) {
                String[] ids = noticeUser.getIds().split(",");
                for (String id : ids) {
                    noticeUserMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<NoticeUser> queryNoticeUser(NoticeUser noticeUser) {
        Result<NoticeUser> result = new Result<>();
        try {
            QueryWrapper<NoticeUser> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            List<NoticeUser> noticeUsers = noticeUserMapper.selectList(wrapper);
            result.setReturnList(noticeUsers);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<NoticeUser> queryNoticeUserByPage(NoticeUser noticeUser) {
        Result<NoticeUser> result = new Result<>();

        try {
            if (noticeUser.getCurrent() == null || noticeUser.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<NoticeUser> wrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(noticeUser.getTitle())){
                wrapper.lambda().like(NoticeUser::getTitle,noticeUser.getTitle());
            }
            if(!StringUtils.isEmpty(noticeUser.getUserId())){
                wrapper.lambda().like(NoticeUser::getUserId,noticeUser.getUserId());
            }

            IPage<NoticeUser> noticeUsers = noticeUserMapper.selectPage(new Page<>(noticeUser.getCurrent(), noticeUser.getSize()), wrapper);
            result.setReturnPage(noticeUsers);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<NoticeUser> queryNoticeUserById(NoticeUser noticeUser) {
        Result<NoticeUser> result = new Result<>();
        try {
            NoticeUser returnNoticeUser = noticeUserMapper.selectById(noticeUser.getNoticeUserId());
            result.setReturnObj(returnNoticeUser);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
